﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using lite;
using Csla;
using Csla.Data;
using Csla.Core;
using System.Collections.ObjectModel;
using OpenExpressApp.ORM;
using OpenExpressApp.MetaAttribute;
using OpenExpressApp.DB.Utils;
using System.Diagnostics;


namespace OpenExpressApp.Module.Library
{
    [Serializable]
    public partial class OrgPositionUsers : GBusinessListBase<OrgPositionUsers, OrgPositionUser>
    {
        #region  Factory Methods

        private OrgPositionUsers()
        {
            /* Require use of factory methods */
            AllowNew = true;
        }

        public static OrgPositionUsers GetChild(OrgPosition parent)
        {
            return DataPortal.FetchChild<OrgPositionUsers>(parent);
        }

        public static OrgPositionUsers NewChild()
        {
            return DataPortal.CreateChild<OrgPositionUsers>();
        }

        protected override object AddNewCore()
        {
            var item = OrgPositionUser.NewChild();
            this.Add(item);
            return item;
        }

        #endregion

        #region  Data Access

        private void Child_Fetch(OrgPosition parent)
        {

            RaiseListChangedEvents = false;

            using (var db = DBHelper.CreateDb(ConnectionStringNames.OpenExpressApp))
            {
                IQuery q = db.Query();
                q.Constrain("OrgPositionId").Equal(parent.Id);
                foreach (OrgPositionUser c in db.Select<OrgPositionUser>(q))
                {
                    this.Add(OrgPositionUser.GetChild(parent, c));
                }
            }

            RaiseListChangedEvents = true;
        }

        #endregion

    }


    [Serializable]
    [Table]
    [BusinessObject("96D8B5F4-A99C-4247-BDC9-DF00F0DEC331"), Label("用户")]
    [NotAllowNew, NotAllowEdit]
    public partial class OrgPositionUser : GBusinessBase<OrgPositionUser>
    {
        #region  Business Methods

        private static PropertyInfo<Guid> IdProperty =
            RegisterProperty(new PropertyInfo<Guid>("Id"));
        [System.ComponentModel.DataObjectField(true, true)]
        [Column, PK]
        [EntityProperty]
        public Guid Id
        {
            get { return GetProperty(IdProperty); }
            set { SetProperty(IdProperty, value); }
        }


        private static PropertyInfo<Guid> OrgPositionIdProperty =
         RegisterProperty(new PropertyInfo<Guid>("OrgPositionId"));
        [Column]
        [EntityProperty]
        [Lookup("OrgPosition")]
        public Guid OrgPositionId
        {
            get { return GetProperty(OrgPositionIdProperty); }
            set { SetProperty(OrgPositionIdProperty, value); }
        }
        [NotUndoable()]
        private OrgPosition _orgPosition;
        public OrgPosition OrgPosition
        {
            get { return _orgPosition; }
            set
            {
                _orgPosition = value;
                PropertyHasChanged("OrgPosition");
            }
        }

        private static PropertyInfo<Guid> UserIdProperty = RegisterProperty(new PropertyInfo<Guid>("UserId"));
        [Column]
        [EntityProperty]
        [Label("用户")]
        [Lookup("User", UseSelectAddCommand = true)]
        public Guid UserId
        {
            get { return GetProperty(UserIdProperty); }
            set { SetProperty(UserIdProperty, value); }
        }
        [NotUndoable()]
        private User _user;
        public User User
        {
            get { return _user; }
            set
            {
                _user = value;
                PropertyHasChanged("User");
            }
        }

        [EntityProperty, ShowInList, Label("编码")]
        public string Code
        {
            get { return _user.Code; }
        }

        [EntityProperty, ShowInList, Label("名称")]
        public string Name
        {
            get { return _user.Name; }
        }


        #endregion

        #region  Factory Methods

        public static OrgPositionUser NewChild()
        {
            return DataPortal.CreateChild<OrgPositionUser>();
        }

        public static OrgPositionUser GetChild(OrgPosition OrgPosition, OrgPositionUser c)
        {
            c.OrgPosition = OrgPosition;
            return DataPortal.FetchChild<OrgPositionUser>(c);
        }

        private OrgPositionUser()
        {
            /* require use of factory methods */
        }


        #endregion

        #region  Data Access

        protected override void Child_Create()
        {
            LoadProperty(IdProperty, Guid.NewGuid());
            ValidationRules.CheckRules();
        }

        private void Child_Fetch(OrgPositionUser data)
        {
            LoadProperty(IdProperty, data.ReadProperty(IdProperty));
            LoadProperty(OrgPositionIdProperty, data.ReadProperty(OrgPositionIdProperty));

            Debug.Assert(null != data.UserId);
            LoadProperty(UserIdProperty, data.UserId);
            _user = User.GetLazy((Guid)data.UserId);
        }

        private void Child_Insert(BusinessBase parent)
        {
            using (var db = DBHelper.CreateDb(ConnectionStringNames.OpenExpressApp))
            {
                db.Insert(this);
            }
        }

        private void Child_Update(BusinessBase parent)
        {
            using (var db = DBHelper.CreateDb(ConnectionStringNames.OpenExpressApp))
            {
                db.Update(this);
            }
        }

        private void Child_DeleteSelf(BusinessBase parent)
        {
            using (var db = DBHelper.CreateDb(ConnectionStringNames.OpenExpressApp))
            {
                db.Delete(this);
            }
        }

        #endregion

        #region  Validation Rules

        protected override void AddBusinessRules()
        {

        }

        #endregion
    }
}